package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.base.RecycleViewDivider;
import com.wnhz.work.yz.bean.MemberReserveBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---会员的预约
 */
public class MemberReserveActivity extends BaseActivity implements View.OnClickListener, BaseRVAdapter.OnItemClickListener {
    private TextView title;
    private RecyclerView my_recycler;
    private static final String TAG = "MemberReserveActivity";

    private List<MemberReserveBean.InfoBean> beanList = new ArrayList<>();
    private BaseRVAdapter<MemberReserveBean.InfoBean> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_reserve);
        getNearstore();
        initData();
        initView();

    }

    private void initData() {

    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("会员的预约");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
//        for (int i = 0; i < 10; i++) {
//            beanList.add(new MemberReserveBean());
//        }

    }

    private void getNearstore() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        Log.i(TAG, MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.UCENTER_CHILDAPPOINTLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    Log.e(TAG, "onSuccess: " + result);
                    Gson gson = new Gson();
                    MemberReserveBean bean = gson.fromJson(result, MemberReserveBean.class);
                    beanList.clear();
                    if ("1".equals(bean.getResult()))
                        beanList = bean.getInfo();

//                    beanList.clear();
//                    JSONObject json = new JSONObject(result);
//                    JSONArray array = json.optJSONArray("info");
//                    if (array != null) {
//                        List<MemberReserveBean> list = new ArrayList<>();
//                        Gson gson = new Gson();
//                        for (int i = 0; i < array.length(); i++) {
//                            MemberReserveBean bean = new MemberReserveBean();
//                            bean = gson.fromJson(array.optString(i), MemberReserveBean.class);
//                            list.add(bean);
//                        }
//                        beanList = list;
//                        mAdapter.updateAdapter(beanList);
//                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
                super.onFinished();
                hideProgress();
                setData();
            }


            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            default:
                break;
        }
    }

    private void setData() {
        my_recycler = (RecyclerView) findViewById(R.id.my_recycler);


        my_recycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new BaseRVAdapter<MemberReserveBean.InfoBean>(this, beanList) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.member_reserve_item;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                ImageView iv_head = holder.getImageView(R.id.iv_head);
                x.image().bind(iv_head, mList.get(position).getPerson_img(), MyApplication.getInstance().shopoptions);
                holder.setTextView(R.id.tv_name, mList.get(position).getRealname());
                holder.setTextView(R.id.tv_authentication, mList.get(position).getIsexamine());
                holder.setTextView(R.id.tv_start_time, mList.get(position).getTime());
                holder.setTextView(R.id.tv_content, "预约商品:" + mList.get(position).getAppoint_goods());
            }
        };
        mAdapter.addItemClickListener(this);
        my_recycler.addItemDecoration(new RecycleViewDivider(this, LinearLayout.HORIZONTAL, R.drawable.line_shape));
        my_recycler.setAdapter(mAdapter);
    }

    @Override
    public void itemSelect(int position) {
        Intent intent = new Intent(this, ReserveMessageActivity.class).putExtra("appo_id", beanList.get(position).getAppo_id()).putExtra("isex", beanList.get(position).getIsexamine());
        startActivity(intent);
    }

}
